package com.xugongzhang.thread;

import com.xugongzhang.dto.Message;
import com.xugongzhang.enums.MessageTypeEnum;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.HashMap;

/**
 * @author X
 * @date 2022/11/26 23:07
 * 该类的对象和某个客户端通信
 */
@Data
@NoArgsConstructor
public class ServerConnectClientThread extends Thread {
    
    private Socket socket;
    
    private String username;

    @Override
    public void run() {
        while (true) {
            System.out.println("服务端持续连接客户端(" + username + ")获取消息");
            try {
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                Message message = (Message) ois.readObject();
                // 根据messageType区分业务，做不同的处理 
                if (message.getMsgType().equals(MessageTypeEnum.GET_ONLINE_FRIEND.getMessageType())) {
                    String onlineUser = ManageServerConnectClientThread.getOnlineUser();
                    Message message1 = new Message();
                    message1.setMsgType(MessageTypeEnum.RET_ONLINE_FRIEND.getMessageType());
                    message1.setContent(onlineUser);
                    ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                    oos.writeObject(message1);
                }
                else if (message.getMsgType().equals(MessageTypeEnum.CLIENT_EXIT.getMessageType())) {
                    System.out.println(message.getSender() + "退出");
                    // 删除用户对应的线程
                    ManageServerConnectClientThread.removeThread(message.getSender());
                    socket.close();
                    // 退出一直循环的run方法
                    break;
                }
                else if (message.getMsgType().equals(MessageTypeEnum.COMM_MSG.getMessageType())) {
                    // 根据getterId获取到他的线程，然后把消息发送给它
                    ServerConnectClientThread getterThread = ManageServerConnectClientThread.getServerConnectClientThread(message.getGetter());
                    ObjectOutputStream oos = new ObjectOutputStream(getterThread.getSocket().getOutputStream());
                    oos.writeObject(message);
                }
                else if (message.getMsgType().equals(MessageTypeEnum.TO_ALL_MSG.getMessageType())) {
                    HashMap<String, ServerConnectClientThread> map = ManageServerConnectClientThread.getMap();
                    map.keySet().stream().filter(item -> !item.equals(message.getSender()))
                            .map(username -> map.get(username).getSocket())
                            .forEach(socket1 -> {
                                try {
                                    new ObjectOutputStream(socket1.getOutputStream())
                                            .writeObject(message);
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            });
                }
                else if (message.getMsgType().equals(MessageTypeEnum.FILE_MSG.getMessageType())) {
                    // 根据getterId获取到对应线程，将message转发
                    ServerConnectClientThread getterThread = ManageServerConnectClientThread.getServerConnectClientThread(message.getGetter());
                    ObjectOutputStream oos = new ObjectOutputStream(getterThread.getSocket().getOutputStream());
                    oos.writeObject(message);
                }
                else {
                    System.out.println("other...");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public ServerConnectClientThread(Socket socket, String username) {
        this.socket = socket;
        this.username = username;
    }
}
